package com.neusoft.weekreport.component.controller;


import com.neusoft.weekreport.component.bizService.RoleRefUserBizService;
import com.neusoft.weekreport.component.req.RoleRefUserByRoleIdReq;
import com.neusoft.weekreport.component.req.RoleRefUserDeleteUserReq;
import com.neusoft.weekreport.component.resp.ResponseBean;
import com.neusoft.weekreport.component.resp.RoleRefUserByRoleIdResp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@EnableTransactionManagement
@RequestMapping("/roleUser")
public class RoleRefUserController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    RoleRefUserBizService roleRefUserBizService;

    //通过roleid查询用户
    @RequestMapping(value = "/role_ref_userInList", method = RequestMethod.GET)
    public ResponseBean<RoleRefUserByRoleIdResp>  getUserInfoListByRoleId(RoleRefUserByRoleIdReq req,HttpServletRequest request) {
        ResponseBean<RoleRefUserByRoleIdResp> responseBean = new ResponseBean<>();
        try{
            RoleRefUserByRoleIdResp resp = roleRefUserBizService.getUserInfoListByRoleId(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(),"查询用户列表成功",resp);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
            logger.error(e.toString());
        }
        return responseBean;

    }

    //通过roleId 、userid 删除取消授权
    @RequestMapping(value = "/role_ref_user",method = RequestMethod.DELETE)
    public ResponseBean  deleteUserInfoByRoleId(@RequestBody  RoleRefUserDeleteUserReq req, HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean<>();
        try{
            roleRefUserBizService.deleteUserInfoByRoleId(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(),"删除用户权限成功",responseBean);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
            logger.error(e.toString());
        }
        return responseBean;
    }

    //通过roleId查询未关联的用户
    @RequestMapping(value = "/role_ref_userNoList",method = RequestMethod.GET)
    public ResponseBean<RoleRefUserByRoleIdResp> getUserNoByRoleId(RoleRefUserByRoleIdReq req,HttpServletRequest request){
        ResponseBean<RoleRefUserByRoleIdResp> responseBean = new ResponseBean<>();
        try{
            RoleRefUserByRoleIdResp resp = roleRefUserBizService.getUserNoByRoleId(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(),"查询未关联用户权限成功",resp);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
            logger.error(e.toString());
        }
        return responseBean;
    }

    //给roleId添加用户
    @RequestMapping(value = "/role_ref_user",method = RequestMethod.POST)
    public ResponseBean AddUserByRoleId(@RequestBody RoleRefUserDeleteUserReq req,HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try{
            roleRefUserBizService.AddUserByRoleId(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(),"给角色添加用户成功",responseBean);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
            logger.error(e.toString());
        }
        return responseBean;
    }



}
